package com.hpin.mlxteacher.message;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.base.BaseActivity;
import com.hpin.mlxteacher.utils.CommonUtils;
import com.hpin.mlxteacher.utils.LogUtil;

/**
 * Created by Aaron on 2015/7/17.
 */
public class SearchActivity extends BaseActivity {

    private EditText et_search;
    private ImageButton ib_search_clear;
    private ListView lv_search_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_news_search);

        initWidget();

    }

    private void initWidget() {

        ImageView iv_back = (ImageView) findViewById(R.id.iv_back);
        et_search = (EditText) findViewById(R.id.et_search);
        ib_search_clear = (ImageButton) findViewById(R.id.ib_search_clear);

        lv_search_result = (ListView) findViewById(R.id.lv_search_result);

        et_search.setImeOptions(EditorInfo.IME_ACTION_SEARCH);

        iv_back.setOnClickListener(this);
        ib_search_clear.setOnClickListener(this);

        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                //判断是否有内容
                ib_search_clear.setVisibility(CommonUtils.isNull(et_search.getText().toString().trim()) ? View.INVISIBLE : View.VISIBLE);

            }
        });


        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {

                    LogUtil.e("TAG", "====search======?");

//                    String search = et_search.getText().toString().trim();
//                    if (search != null && !search.equals("")) {
//                        try {
//                            search = URLEncoder.encode(search, "utf-8");
//                            getSearchInfo(search);
//                        } catch (UnsupportedEncodingException e) {
//                            showToast("编码异常");
//                        }
//                    } else {
//                        // nowPage = 0;
//                        // getHumanResourceList(1);
//                        showToast("请输入检索条件");
//                    }
                    return true;
                }
                return false;
            }

        });
    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_back :
                //返回
                finish();

                break;

            case R.id.ib_search_clear :

                //清除筛选内容
                et_search.setText("");

                break;

        }
    }


}
